package helpers;

import java.net.URLEncoder;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class CrawlerHelper {
	public static String rewriteArticle(String content) {
		String result = "";
		
		String url = "http://www.ezrewrite.com/rewrite/rewrite.php";
		
		try {
			Connection conn = Jsoup.connect(url);
			
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("host", "www.ezrewrite.com")
					.header("referer", url)
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
					.data("article", content)
					.data("submit", "Rewrite Article")
					.followRedirects(false)
					.post();
			
			Response response = conn.response();
			
			doc = Jsoup.parse(response.body());
			
			doc.select("ins, script").remove();
			
			result = doc.select("p").html();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return result;
	}
	
	public static String getTag(String content) {
		String result = "";
		
		String url = "http://generatetagger.appspot.com/create-tag";
		
		try {
			Connection conn = Jsoup.connect(url);
			
			Document doc = conn
					.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("accept-Language", "en-us,en;q=0.5")
					.header("host", "generatetagger.appspot.com")
					.header("referer", url)
					.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1")
					.data("content", content)
					.followRedirects(false)
					.post();
			
			Response response = conn.response();
			
			result = response.body();
			
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		String content = "Direct Answers - Column for the week of October 6, 2003 It took me eight years after my husband died to try a relationship, and now I am thinking maybe I should never have tried. Over a year ago I met Jay. I always felt we had the perfect relationship. We like the same things, have the same type personality, and talked things through when there was a misunderstanding. A few days ago Jay came over, and after dinner he said he doesn't want to see me anymore. The next day he left a phone message saying he knows we have a relationship most people spend a lifetime searching for. He said he loves me and still wants me in his life, but to think of this as a break from each other and not a breakup. I spent the last week crying and upset. Do I sit around and wait for him to call, and if he does, start the relationship over? Does this mean every time there is a difficult situation in his life he will push me out of it? Is there a chance there is more to this than he is saying? Caitlin Caitlin, a relationship people spend a lifetime searching for is not a relationship one walks away from without giving a reason. Jay broke up with you in a way which makes it easy on himself and hard on you. Then he put you in a holding pattern while leaving all his options open. As I waited for Tamara outside a shop today, I thought I saw a few falling leaves. Then I realized they were not leaves, but Monarch butterflies. The Monarchs' flight is a bit erratic anyway, and the wind blew them around like leaves. These butterflies are on their long migration from Canada to central Mexico. The Monarchs know when it is time for their journey to begin, and that is why you started dating again. It was time. Like the Monarchs, your flight may be a little erratic before you reach your true destination. Jay was just a little turbulence along the way. Wayne This Or That I truly felt I found Mr. Right. He's one of the few men I know who are considerate, open, and willing to make a difference in the world. He says he has chosen a lifestyle which is not the typical American one of over-consumption, designer brands, and expensive things. I've moved to the city where he lives, staying with him until I find a room and a job. He says he wants for us to be in the same city, for me to be working and living my own life so he knows what we are like as a couple. Recently I saw an e-mail he sent a friend. I know I broke the trust tenet by reading it, and I don't know why I chose to read this particular e-mail, but it sounds like he wants to keep an open channel with her for possible future opportunities. I felt love for him before I found out. Now I don't know if I love him or not. I feel lucky to have found someone, but I don't know if love will ever grow in his heart for me. Beryl Beryl, you don't sound like someone who normally snoops. Something in his demeanor caused you to look, and in looking you have exchanged one old saying for another. You have lost ignorance is bliss and gained knowledge is power. But knowledge is power only if you act on it. You attempted to verify one of two statements: he loves me, or he loves me not. Now you have the answer. If you allow him to determine the course of your relationship, he has all the power. He will do what he will do. That will not lead him to treat you with respect because you are not treating yourself with respect. Tamara";
		
		System.out.println(getTag(content));
		
		
	}
}
